//
//  SelectCountryAndAreaViewController.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/4.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "SelectCountryAndAreaViewController.h"
#import "XXTool.h"
#import "CellForSelectCountryAndArea.h"

@interface SelectCountryAndAreaViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic) UITableView *tableview;

@end

@implementation SelectCountryAndAreaViewController
{
    NSMutableArray *areaCodeArray;
    DKSortArray *sortArray;
    NSMutableArray *sortUseData;
}

-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.separatorColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:222/255.0 alpha:1.0];
        _tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableview.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:240/255.0 alpha:1.0];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.tintColor = [UIColor colorWithRed:59/255.0 green:58/255.0 blue:62/255.0 alpha:1.0];
        _tableview.sectionIndexBackgroundColor = [UIColor clearColor];
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

-(void)initParameter
{
    sortArray = [[DKSortArray alloc] init];
    sortUseData = [[NSMutableArray alloc] init];
    sortArray.dataSource = self;
    
    areaCodeArray = [[AreaCodeDataSqlite shareMyData] getAllAreaCode];
    
    
    if ([XXTool didCurrentLanguageChinese])
    {
        for (int i=0; i<areaCodeArray.count; i++)
        {
            AreaCodeDBModel *use = areaCodeArray[i];
            if ([use.countryC isEqualToString:@"中国"])
            {
                [areaCodeArray removeObjectAtIndex:i];
                break;
            }
        }
    }
   
    
    for (int i=0; i<areaCodeArray.count; i++)
    {
        AreaCodeDBModel *use = areaCodeArray[i];
        if ([XXTool didCurrentLanguageChinese])
        {
            [sortUseData addObject:use.countryC];
        }
        else
        {
            [sortUseData addObject:use.countryE];
        }
    }
    [sortArray setArray:sortUseData];
    [sortArray sort];
    
    [_tableview reloadData];
}

-(void)initUI
{
    self.view.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:240/255.0 alpha:1.0];
    [self.navigationItem setTitle:NSLocalizedString(@"选择国家和地区", nil)];
    [self tableview];
    
}

-(void)refreshLayout:(CGRect)frame
{
    _tableview.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
}

-(BOOL)navigationShouldPopOnBackButton
{
    _tempBlock(@"");  //用户没有选择任何一个区号，点击了返回按钮
    return YES;
}

#pragma mark SortArrayDataSource
//DKSortDataSoure
-(NSString*)DKSortArray:(DKSortArray *)object stringOfSortArray:(NSArray *)dataArray AtIndex:(NSUInteger)index
{
    return sortUseData[index];
}

#pragma mark TableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if ([XXTool didCurrentLanguageChinese])
    {
        if (indexPath.section==0)
        {
            _tempBlock(@"86");
        }
        else
        {
            AreaCodeDBModel *temp = areaCodeArray[[sortArray indexOfSection:indexPath.section-1 AtIndex:indexPath.row]];
            _tempBlock(temp.areaCode);
        }
    }
    else
    {
        AreaCodeDBModel *temp = areaCodeArray[[sortArray indexOfSection:indexPath.section AtIndex:indexPath.row]];
        _tempBlock(temp.areaCode);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark TaleViewDataSource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *reuseIdentifer = @"CellForSelectCountryAndArea";
    CellForSelectCountryAndArea *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifer];
    if (cell==nil)
    {
        cell = [[CellForSelectCountryAndArea alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifer];
    }
    if ([XXTool didCurrentLanguageChinese])
    {
        if (indexPath.section==0)
        {
            AreaCodeDBModel *use = [[AreaCodeDBModel alloc] init];
            use.countryC = @"中国";
            use.countryE = @"中国";
            use.areaCode = @"86";
            [cell setInfo:[[NSDictionary dictionaryWithObject:use forKey:cellForSelectCountryAndAreaKey] mutableCopy]];
        }
        else
        {
            [cell setInfo:[[NSDictionary dictionaryWithObject:areaCodeArray[[sortArray indexOfSection:indexPath.section-1 AtIndex:indexPath.row]] forKey:cellForSelectCountryAndAreaKey] mutableCopy]];
        }
      
    }
    else
    {
        [cell setInfo:[[NSDictionary dictionaryWithObject:areaCodeArray[[sortArray indexOfSection:indexPath.section AtIndex:indexPath.row]] forKey:cellForSelectCountryAndAreaKey] mutableCopy]];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if ([XXTool didCurrentLanguageChinese])
    {
        return [sortArray numberOfSection]+1;
    }
    else
    {
        return [sortArray numberOfSection];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([XXTool didCurrentLanguageChinese])
    {
        if (section==0)
        {
            return 1;
        }
        else
        {
            return [sortArray numberOfRowAtSection:section-1];
        }
    }
    else
    {
        return [sortArray numberOfRowAtSection:section];
    }
}

-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [sortArray getTitleArray];
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
    if (sectionTitle == nil) {
        return  nil;
    }
    
    UILabel * label = [[UILabel alloc] init];
    label.textColor = [UIColor grayColor];
    label.frame = CGRectMake(5, 0, self.view.frame.size.width-15, 35/2);
    label.font=[XXTool systemFontOfSize:14];
    label.text = sectionTitle;
    
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0
                                                                    , 0
                                                                    , self.view.frame.size.width-15
                                                                    , 35/2)];
    [sectionView setBackgroundColor:self.view.backgroundColor];
    [sectionView addSubview:label];
    return sectionView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35/2;
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if ([XXTool didCurrentLanguageChinese])
    {
        if (section==0)
        {
            return nil;
        }
        else
        {
            return [[sortArray getTitleArray] objectAtIndex:section-1];
        }
        
    }
    else
    {
        return [[sortArray getTitleArray] objectAtIndex:section];
    }
}

+(void)selectCountryAndAreaViewControllerDidPushWithSourceViewControoler:(UIViewController *)vc Compeletation:(void (^)(NSString *))block
{
    SelectCountryAndAreaViewController *selfVC = [[SelectCountryAndAreaViewController alloc] init];
    selfVC.tempBlock = block;
    if (vc)
    {
        [vc.navigationController pushViewController:selfVC animated:YES];
    }
}

@end
